<template>
  <div class="photolist">
    <!-- 顶部滑动条 -->
    <div id="slider" class="mui-slider">
      <div
        id="sliderSegmentedControl"
        class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted"
      >
        <div class="mui-scroll">
          <a
            :class="['mui-control-item',item.id==0 ? 'mui-active' : '']"
            v-for="item in categories"
            :key="item.id"
            @click="getPhotoListById(item.id)"
          >{{item.title}}</a>
        </div>
      </div>
    </div>

    <!-- 图片列表区域 -->
     <ul class="photo-list">
            <router-link v-for="item in list" :to="'/home/photoinfo/'+item.id" tag="li" :key="item.id">
                <img v-lazy="item.img_url">
                <div class="photo-info">
                    <h3 class="info-title">{{item.title}}</h3>
                    <p class="info-body">{{item.zhaiyao}}</p>
                </div>
            </router-link>
        </ul>
  </div>
</template>

<script>
// 1.导入mui.hs文件
import mui from "../../lib/mui/js/mui.min.js";
import { Toast } from "mint-ui";
export default {
  data() {
    return {
      categories: [],
      list: [], //图片列表的数组
    };
  },
  created() {
    this.getCategory();
    this.getPhotoListById(0);
  },
  mounted() {
    //当组件中的DOM结构被渲染好并放入到页面中，会执行这个函数
    // 2.若使用区域滚动组件，需手动初始化scroll控件
    mui(".mui-scroll-wrapper").scroll({
      deceleration: 0.0005, //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
    });
  },
  methods: {
    getCategory() {
      var _this = this;
      _this.$http
        .get("http://api.cms.liulongbin.top/api/getimgcategory")
        .then(function (res) {
          if (res.data.status === 0) {
            res.data.message.unshift({ title: "全部", id: 0 });
            _this.categories = res.data.message;
            _this.categories.splice(1,4)
          } else {
            Toast("所有分类加载失败！");
          }
        })
        .catch(function (err) {
          console.log(err);
        });
    },
    //获取所有图片
    getPhotoListById(cateId) {
      this.$http
        .get("http://api.cms.liulongbin.top/api/getimages/" + cateId)
        .then((result) => {
          if (result.data.status === 0) {
            this.list = result.data.message;
          } else {
            Toast("图片获取失败！");
          }
        });
    },
  },
watch: {
   'this.$route.path' (val, old) {
        // 当前路由
        if(val.path == '/home/photolist'){
         location.reload();
     
        }
    }
    },
};
</script>

<style lang="scss" scoped>
* {
  touch-action: pan-y;
}
   .photo-list{
        list-style: none;
        padding: 10px;
        padding-bottom: 0;
        margin: 0;
        text-align: center;
        box-shadow: 0 0 9px #999;
        li{
            background-color: #ccc;
            margin-bottom: 10px;
            position: relative;
            img{
                width: 100%;
                vertical-align: middle;
            }
            img[lazy=loading] {
                width: 40px;
                height: 300px;
                margin: auto;
            }
            .photo-info{
                text-align: left;
                color: white;
                position: absolute;
                bottom: 0;
                background-color: rgba(0,0,0,0.4);
                max-height: 84px;
                .info-title{
                    font-size: 14px;
                }
                .info-body{
                    font-size: 13px;
                    color: white;
                }
            }

        }
    }
</style>